/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.web.firefox;

import java.io.File;
import java.util.List;

import org.kommando.web.WebPage;
import org.kommando.web.firefox.bookmarks.Bookmarks;
import org.kommando.web.firefox.mozrepl.MozRepl;

/**
 * Abstraction of the Firefox application.
 * 
 * @author Peter De Bruycker
 */
public class Firefox {

    // TODO error handling: mozRepl not running -> connect refused

    private Bookmarks bookmarks;

    private String getProfileFolder() {
        MozRepl mozRepl = new MozRepl(MozRepl.DEFAULT_HOST, MozRepl.DEFAULT_PORT);
        try {
            mozRepl.connect();
            return mozRepl
                    .sendAndReceive("Components.classes[\"@mozilla.org/file/directory_service;1\"].getService( Components.interfaces.nsIProperties).get(\"ProfD\", Components.interfaces.nsIFile).path");
        } finally {
            if (mozRepl.isConnected()) {
                mozRepl.disconnect();
            }
        }
    }

    public List<WebPage> getBookmarks() {
        if (bookmarks == null) {
            bookmarks = new Bookmarks(new File(getProfileFolder(), "bookmarks.html"));
        }

        return bookmarks.getBookmarks();
    }

    public WebPage getCurrentWebPage() {
        MozRepl mozRepl = new MozRepl(MozRepl.DEFAULT_HOST, MozRepl.DEFAULT_PORT);
        try {
            mozRepl.connect();

            StringBuilder sb = new StringBuilder();
            sb.append("var output = content.document.title + '\\n' + content.document.URL + '\\n'; ");
            sb.append("for (i = 0; i < content.document.links.length; i++) { ");
            sb.append("    link = content.document.links.item(i); ");
            sb.append("    output += link.text + '\\n'; ");
            sb.append("    output += link.href + '\\n'; ");
            sb.append("}");
            String response = mozRepl.sendAndReceive(sb.toString());

            String[] lines = response.split("\\n");
            String title = lines[0];
            String url = lines[1];
            WebPage webPage = new WebPage(title, url);

            // children
            // for (int i = 2; i < lines.length / 2; i++) {
            // String linkTitle = lines[i + i - 2];
            // String linkURL = lines[i + i - 1];
            //
            // webPage.addLink(new WebPageLink(linkTitle, linkURL));
            // }

            return webPage;
        } finally {
            if (mozRepl.isConnected()) {
                mozRepl.disconnect();
            }
        }
    }

}
